package cn.stylefeng.guns.dao.impl;

import cn.stylefeng.guns.dao.ISaleStockService;
import cn.stylefeng.guns.dao.entity.ReportDto;
import cn.stylefeng.guns.dao.entity.SaleStock;
import cn.stylefeng.guns.dao.entity.Supplier;
import cn.stylefeng.guns.dao.mapper.SaleStockMapper;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author quspacedraon
 * @since 2024-01-13
 */
@Service
public class SaleStockServiceImpl extends ServiceImpl<SaleStockMapper, SaleStock> implements ISaleStockService {
    @Override
    public Boolean isExist(String batchNo, String commodityCode) {
        return this.count(Wrappers.<SaleStock>lambdaQuery().eq(SaleStock::getBatchNo, batchNo).eq(SaleStock::getGoodsId, commodityCode)) > 0;
    }

    @Override
    public List<ReportDto> sumTotal(Long startTime, Long endTime, String supplierId, String goodsId, List<String> saleStockIdList) {
        return getBaseMapper().sumPrice(startTime, endTime, supplierId, goodsId, saleStockIdList);
    }

    @Override
    public Map<String, Double> sumTotalPrice(Long startTime, Long endTime, List<String> supplierIdList) {
        List<Map<String, Object>> maps = getBaseMapper().sumTotalPrice(startTime, endTime, supplierIdList);
        Map<String, Double> collect = maps.stream()
                .collect(Collectors.toMap(r -> Objects.nonNull(r.get("supplierId")) ? r.get("supplierId").toString() : "", r -> {
                    Object price = r.get("price");
                    if (Objects.isNull(price)) {
                        return 0D;
                    } else {
                        return ((BigDecimal) price).doubleValue();
                    }
                }, (a, b) -> a));
        return collect;
    }
}
